import {
  getAutoScore,
  getScoreQuntile,
  getLoseStats,
  getCoreData,
  getDailyScore,
  getBanbieScore,
  getScoreTrend,
  getHistory,
  downloadHistory,
  getRules
} from '@/services/gaolu';
import time, {currentTime} from "@/utils/time";


const initState = {
  autoScore: null,
  scoreQuntile: null,
  paramStatus: null,
  scoreTrend: null,
  globalTime : {
    startOfDay: time.getStartOfDay(),
    endOfDay: time.getEndOfDay(),
    currentTime: currentTime()
  },
  gaoluRules:[]
};

const GaoLu = {
  namespace: 'gaolu',
  state: initState,
  effects: {
    //异步方法,应该通常用于前后端交互
    * autoScore({payload}, {call, put}) {
      const response = yield call(getAutoScore, payload);
      // dispatch转发action
      yield put({
        type: 'saveAutoScore',
        payload: response.detail,
      });
    },
    * getScoreQuntile({payload}, {call, put}) {
      const response = yield call(getScoreQuntile, payload);
      // dispatch转发action
      yield put({
        type: 'saveScoreQuntile',
        payload: response.detail,
      });
    },
    * getLoseStatsEffect({payload}, {call, put}) {
      const response = yield call(getLoseStats, payload);
      // dispatch转发action
      yield put({
        type: 'saveLoseStats',
        payload: response.detail,
      });
    },

    * getCoreDataEffect({payload}, {call, put}) {
      const response = yield call(getCoreData, payload);
      // dispatch转发action
      yield put({
        type: 'saveCoreData',
        payload: response.detail,
      });
    },
    * getDailyScoreEffect({payload}, {call, put}) {
      const response = yield call(getDailyScore, payload);
      // dispatch转发action
      yield put({
        type: 'saveDailyScore',
        payload: response.detail,
      });
    },
    * getBanbieEffect({payload}, {call, put}) {
      const response = yield call(getBanbieScore, payload);
      // dispatch转发action
      yield put({
        type: 'saveBanbieScore',
        payload: response.detail,
      });
    },
    * getScoreTrendEffect({payload}, {call, put}) {
      const response = yield call(getScoreTrend, payload);
      // dispatch转发action
      yield put({
        type: 'saveScoreTrend',
        payload: response.detail,
      });
      debugger
      yield put({
        type: 'saveGlobalTime',
        payload: payload.globalTime,
      });
    },
    * getHistory({payload}, {call, put}) {
      const response = yield call(getHistory, payload);
      // dispatch转发action
      yield put({
        type: 'saveHistory',
        payload: response.detail,
      });
    },
    * downloadHistory({payload,callback}, {call, put}) {
      const response = yield call(downloadHistory, payload);
      // dispatch转发action

      if (response instanceof Blob) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
      } else {
        message.warning('Some error messages...', 5);
      }
    },
    * getGaoluRules({payload}, {call, put}) {
      const response = yield call(getRules, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: 'gaoluRules'
      });
    }

  },

  reducers: {
    //同步方法
    saveAutoScore(state, {payload}) {
      return {...state, autoScore: payload};
    },

    saveScoreQuntile(state, {payload}) {
      // dispatch转发action
      return {...state, scoreQuntile: payload};
    },

    saveLoseStats(state, {payload}) {
      return {...state, paramStatus: payload};
    },
    saveCoreData(state, {payload}) {
      return {...state, coreData: payload};
    },
    saveDailyScore(state, {payload}) {
      return {...state, daliyScore: payload};
    },
    saveBanbieScore(state, {payload}) {
      return {...state, banbieScore: payload};
    },
    saveScoreTrend(state, {payload}) {
      return {...state, scoreTrend: payload};
    },
    saveHistory(state, {payload}) {
      return {...state, predictHistory: payload};
    },
    saveGlobalTime(state,{payload}) {
      return {...state,globalTime:payload}
    },
    saveValues(state, {payload, name}) {
      const v = {...state};
      v[name] = payload;
      return v
    },
  },
};
export default GaoLu;
